package juliusapp.dao;

import java.util.ArrayList;
import java.util.List;

import juliusapp.dao.db.DB;
import juliusapp.model.UserVO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDAO {

	private static String tableName = "user";
	private SQLiteDatabase db;
	private ContentValues ctv;
	private String[] collumns = new String[] { "id", "name", "email",
			"password" };

	protected UserDAO(Context ctx) {
		db = new DB(ctx).getWritableDatabase();
	}

	protected void insert(UserVO vo) throws Exception {
		try {
			ctv = new ContentValues();
			ctv.put("name", vo.getNome());
			ctv.put("email", vo.getEmail());
			ctv.put("password", vo.getPassword());
			db.insert(tableName, null, ctv);
		} catch (Exception e) {
			throw new Exception("insert|" + e.getMessage());
		}
	}

	protected void delete(UserVO vo) throws Exception {
		try {
			db.delete(tableName, "id=?", new String[] { vo.getId().toString() });
		} catch (Exception e) {
			throw new Exception("delete|" + e.getMessage());
		}
	}

	protected void update(UserVO vo) throws Exception {
		try {
			ctv = new ContentValues();
			ctv.put("id", vo.getId());
			ctv.put("name", vo.getNome());
			ctv.put("email", vo.getEmail());
			ctv.put("password", vo.getPassword());
			db.update(tableName, ctv, "id=?", new String[] { vo.getId().toString()});

		} catch (Exception e) {
			throw new Exception("update|" + e.getMessage());
		}
	}

	protected UserVO findById(Integer id) throws Exception {
		try {
			Cursor rs = db.query(tableName,collumns,"id=?", new String[]{id.toString()},null,null,null);
			UserVO vo= new UserVO();
			
			if(rs.moveToFirst()){
				vo.setId(rs.getInt(rs.getColumnIndex("id")));
				vo.setName(rs.getString(rs.getColumnIndex("name")));
				vo.setEmail(rs.getString(rs.getColumnIndex("email")));
				vo.setPassword(rs.getString(rs.getColumnIndex("password")));
			}
			
			return vo;

		} catch (Exception e) {
			throw new Exception("findById|" + e.getMessage());
		}

	}
	
	/**
	 * Método utilizado para validar se existe um usuário cadastrado na base com o login e senha informados.
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	protected boolean Login(UserVO vo) throws Exception {
		try {
			Cursor rs = db.query(tableName,collumns,"email=? and password=?", new String[]{vo.getEmail(),vo.getPassword()},null,null,null);
			return (rs.moveToFirst());

		} catch (Exception e) {
			throw new Exception("login|" + e.getMessage());
		}

	}
	
	protected boolean existEmail(String email) throws Exception {
		try {
			Cursor rs = db.rawQuery("SELECT * FROM user where email LIKE '%"+email+"%'", null);
			return rs.moveToFirst();

		} catch (Exception e) {
			throw new Exception("existEmail|" + e.getMessage());
		}

	}

	protected List<UserVO> find() throws Exception {
		try {
			Cursor rs = db.rawQuery("SELECT * FROM user", null);
			UserVO vo;
			List<UserVO> list= new ArrayList<UserVO>();
			
			while(rs.moveToNext()){
				vo= new UserVO();
				vo.setId(rs.getInt(rs.getColumnIndex("id")));
				vo.setName(rs.getString(rs.getColumnIndex("name")));
				vo.setEmail(rs.getString(rs.getColumnIndex("email")));
				vo.setPassword(rs.getString(rs.getColumnIndex("password")));
				list.add(vo);
			}
			
			return list;

		} catch (Exception e) {
			throw new Exception("find|" + e.getMessage());
		}
	}

}
